package com.hb.trade.api.routine.controller.center;

import cn.hutool.json.JSONObject;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.center.au.dto.UserExistsDTO;
import com.hb.core.pojo.center.au.dto.ZappUserDTO;
import com.hb.core.pojo.enums.RoleSignEnum;
import com.hb.trade.api.routine.service.ExpertUserService;
import com.hb.trade.api.routine.service.RsaAlgorithmService;
import com.hb.trade.api.routine.sms.annotation.SmsSend;
import com.hb.trade.client.center.UserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.service.UserEntityService;
import org.fzzn.core.auth.shiro.util.CredentialUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.third.sms.service.SmsService;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户登录相关
 *
 * @author zhaojk
 */
@Slf4j
@Version(1.0)
@Api(tags = {"系统用户注册相关接口"})
@RequestMapping("/{version}")
@RestController
public class RegisterController {
    protected final UserClient userClient;
    protected final RsaAlgorithmService rsaAlgorithmService;
    protected final UserEntityService userEntityService;
    protected final SmsService smsService;
    protected final ExpertUserService expertUserService;

    public RegisterController(UserClient userClient,
                              RsaAlgorithmService rsaAlgorithmService,
                              UserEntityService userEntityService, SmsService smsService, ExpertUserService expertUserService) {
        this.userClient = userClient;
        this.userEntityService = userEntityService;
        this.rsaAlgorithmService = rsaAlgorithmService;
        this.smsService = smsService;
        this.expertUserService = expertUserService;
    }

    @ApiOperation(value = "判断用户名是否重复", httpMethod = "POST")
    @PostMapping("/user/userExists")
    public ResponseDto userExists(@RequestBody UserExistsDTO userExistsDTO) {
        String userName = userExistsDTO.getUserName();
        if (userName == null) {
            return ResultUtil.fail(MsgEnum.USERNAME_NOT_ALLOW_EMPTY);
        }
        Message<UserDTO> tradeUser = userClient.checkUsername(userExistsDTO);
        if (tradeUser.getData() == null) {
            return ResultUtil.msg(true);
        }
        return ResultUtil.fail("该用户已存在");
    }

    @ApiOperation(value = "获取加密公钥", notes = "获取加密公钥", httpMethod = "GET")
    @GetMapping("/user/getPubKey")
    public ResponseDto getPubKey() {
        String pubKey = rsaAlgorithmService.getPubKey();
        JSONObject obj = new JSONObject();
        obj.putOpt("pubKey", pubKey);
        return ResultUtil.msg(true, obj);
    }

    /**
     * TODO 接口前端未使用
     * @param zappUserDTO
     * @return
     */
    @Deprecated
    @ApiOperation(value = "招标人注册接口", notes = "目前只实现了中招扫码注册和手动填写企业信息", httpMethod = "POST")
    @SmsSend(type = 2)
    @PostMapping("/user/registerbyZapp")
    public ResponseDto registerByZapp(@RequestBody ZappUserDTO zappUserDTO) {
        // 获取用户信息
        UserDTO userDTO = zappUserDTO.getUser();
        // 用户名密码不能为空
        String userName = userDTO.getUsername();
        if (userName == null) {
            return ResultUtil.fail(MsgEnum.USERNAME_NOT_ALLOW_EMPTY);
        }
        String password = userDTO.getPassword();
        if (password == null) {
            return ResultUtil.fail(MsgEnum.PASSWORD_NOT_ALLOW_EMPTY);
        }
        // 设置密码加密的盐
        String saltSource = userDTO.getSaltSource();
        if (saltSource == null) {
            saltSource = UserDTO.INIT_SALT_VALUE;
            zappUserDTO.setSaltSource(saltSource);
        }
        // 利用RSA解密密码
//        String decryptPassword = rsaAlgorithmService.decryptData(zappUserDTO.getPassword());

        // 将密码加密
        String antiPassword = CredentialUtil.md5(zappUserDTO.getPassword(), saltSource);
        zappUserDTO.setPassword(antiPassword);

        Message<UserDTO> message = userClient.registerZappUser(zappUserDTO);

        // 初始化专家库信息
/*        if (message.isSuccess()) {
            UserDTO data = message.getData();
            expertUserService.createExpertUser(data.getOrgId(), data.getOrgName());
        }*/

        return ResultUtil.msg(message);
    }

    @ApiOperation(value = "投标人人注册接口", notes = "目前只实现了中招扫码注册", httpMethod = "POST")
    @SmsSend(type = 1)
    @PostMapping("/user/registerTenderbyZapp")
    public ResponseDto registerTenderbyZapp(@RequestBody ZappUserDTO zappUserDTO) {
        // 获取用户信息
        UserDTO userDTO = zappUserDTO.getUser();
        // 用户名密码不能为空
        String userName = userDTO.getUsername();
        if (userName == null) {
            return ResultUtil.fail(MsgEnum.USERNAME_NOT_ALLOW_EMPTY);
        }
        String password = userDTO.getPassword();
        if (password == null) {
            return ResultUtil.fail(MsgEnum.PASSWORD_NOT_ALLOW_EMPTY);
        }
        // 设置密码加密的盐
        String saltSource = userDTO.getSaltSource();
        if (saltSource == null) {
            saltSource = UserDTO.INIT_SALT_VALUE;
            zappUserDTO.setSaltSource(saltSource);
        }

//        String decryptPassword = rsaAlgorithmService.decryptData(zappUserDTO.getPassword());

        // 将密码加密
        String antiPassword = CredentialUtil.md5(zappUserDTO.getPassword(), saltSource);
        zappUserDTO.setPassword(antiPassword);
        // 保存用户信息
        Message<UserDTO> message = userClient.registerZappUser(zappUserDTO);

        // 注册角色为代理机构或者招标人时添加私有专家库帐号
        Integer roleSign = zappUserDTO.getRoleSign();
        if (Enums.equals(RoleSignEnum.AGENCY, roleSign)
                || Enums.equals(RoleSignEnum.TENDEREE, roleSign)) {
            // 初始化专家库信息
            if (message.isSuccess()) {
                UserDTO data = message.getData();
                expertUserService.createExpertUser(data.getOrgId(), data.getOrgName());
            }
        }

        return ResultUtil.msg(message);
    }
}
